<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增延期任务')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-task-delay-add">
        <div class="col-md-12">
            <div class="form-group">
                <label class="col-sm-2 control-label">选择任务：</label>
                <div class="col-sm-9">
                    <input type="text" required maxlength="50" name="taskId" id="taskId" class="form-control" placeholder="请输入要延期的任务编号">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">申请人：</label>
                <div class="col-sm-3">
                    <select class="form-control" name="comp" id="comp">
                        <option value="">请选择公司</option>
                    </select>
                </div>
                <div class="col-sm-3">
                    <select class="form-control" name="dept" id="dept">
                        <option value="">请选择部门</option>
                    </select>
                </div>
                <div class="col-sm-3">
                    <select class="form-control" name="applicant" id="applicant" required>
                        <option value="">请选择申请人</option>
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-2 control-label">申请原因：</label>
                <div class="col-sm-9">
                    <textarea type="text" style="height: 200px" name="delayReason" id="delayReason" class="form-control" placeholder="请输入申请原因" required maxlength="255"></textarea>
                </div>
            </div>
        </div>
    </form>
</div>
<th:block th:include="include :: footer" />
<th:block th:include="include :: summernote-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script type="text/javascript">
    var prefix = ctx + "system/task/delay";

    $("#form-task-delay-add").validate({
        rules:{
            onkeyup: false,
            taskId:{
                remote: {
                    url: ctx + "system/task/delay/checkDelayTaskIdUnique",
                    type: "post",
                    dataType: "json",
                    data: {
                        "taskId" : function() {
                            return $.common.trim($("#taskId").val());
                        }
                    },
                    dataFilter: function(data, type) {
                        return $.validate.unique(data);
                    }
                },
                digits:true
            }
        },
        messages: {
            "taskId": {
                remote: "该任务不存在或已是延期任务"
            },
        },
        focusCleanup: true
    });

    function getApplicant() {
        $.ajax({
            url: "/system/dept/selectCompAll",
            success: function (result) {
                $.each(result, function (index, value) {
                    var name = value.deptName;
                    var id = value.deptId;
                    $('#comp').append("<option value='" + id + "'>" + name + "</option>");
                });
                $("#comp").selectpicker('refresh');
            }
        });
        $("#comp").bind("change", function() {
            //刷新选中第一条空数据
            document.getElementById("dept").options.selectedIndex = 0;
            document.getElementById("applicant").options.selectedIndex = 0;
            //只保留下拉框中第一条空数据
            document.getElementById("dept").options.length = 1;
            document.getElementById("applicant").options.length = 1;
            $("#applicant").selectpicker('refresh');
            var deptId = $(this).val(); //获取当前下拉框name属性的值
            console.log(deptId);
            $.ajax({
                type : "POST",
                url : "/system/dept/selectDeptAll",
                data : {
                    "deptId" : deptId
                },
                success : function(result) {
                    $.each(result, function (index, value) {
                        var name = value.deptName;
                        var id = value.deptId;
                        $('#dept').append("<option value='" + id + "'>" + name + "</option>");
                    });
                    $("#dept").selectpicker('refresh');
                },
            });
        });
        $("#dept").bind("change", function() {
            //刷新选中第一条空数据
            document.getElementById("applicant").options.selectedIndex = 0;
            //只保留下拉框中第一条空数据
            document.getElementById("applicant").options.length = 1;
            var deptId = $(this).val(); //获取当前下拉框name属性的值
            console.log(deptId);
            $.ajax({
                type : "POST",
                url : "/system/user/selectUsersBydeptId",
                data : {
                    "deptId" : deptId
                },
                success : function(result) {
                    $.each(result, function (index, value) {
                        var name = value.userName;
                        var id = value.userId;
                        $('#applicant').append("<option value='" + id + "'>" + name + "</option>");
                    });
                    $("#applicant").selectpicker('refresh');
                },
            });
        });
    }
    getApplicant();


    function submitHandler() {
        if ($.validate.form()) {
            $.operate.save(prefix + "/add",$("#form-task-delay-add").serialize());
        }
    }

</script>
</body>
</html>
